package com.excellent.system.controller;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.service.ISysTaskStuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.excellent.common.annotation.Log;
import com.excellent.common.enums.BusinessType;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.service.ISysTaskStuService;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.common.core.page.TableDataInfo;

/**
 * 审核课题Controller
 *
 * @author ruoyi
 * @date 2022-05-11
 */
@Controller
@RequestMapping("/system/teachecktask")
public class SysTaskStuCheckController extends BaseController
{
    private String prefix = "system/teachecktask";

    @Autowired
    private ISysTaskStuService sysTaskStuService;

    @GetMapping()
    public String teachecktask()
    {
        return prefix + "/teachecktask";
    }

    /**
     * 查询审核课题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTaskStu sysTaskStu)
    {
        SysUser user = ShiroUtils.getSysUser();
        if(user.isAdmin()){   //如果当前是管理员登录显示所有的数据
            if (sysTaskStu.getTaskTerm().equals("")) {
                Calendar c = Calendar.getInstance();  //获取系统当前年份
                int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
                int year = c.get(Calendar.YEAR);
                if (month >= 1 && month <= 6) {
                    sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                } else {
                    sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                }
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            } else {
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            }
        }
        else {  //如果是普通教师只查询和自己双选的同学
            sysTaskStu.setTeaId(Long.parseLong(user.getLoginName()));
            if (sysTaskStu.getTaskTerm().equals("")) {
                Calendar c = Calendar.getInstance();  //获取系统当前年份
                int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
                int year = c.get(Calendar.YEAR);
                if (month >= 1 && month <= 6) {
                    sysTaskStu.setTaskTerm((year-1)+"-"+(year) + "学年春季学期");
                } else {
                    sysTaskStu.setTaskTerm((year)+"-"+(year + 1) + "学年秋季学期");
                }
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            } else {
                startPage();
                List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
                Collections.sort(list);
                return getDataTable(list);
            }
        }
    }

    /**
     * 导出审核课题列表
     */
    @Log(title = "审核课题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTaskStu sysTaskStu)
    {
        List<SysTaskStu> list = sysTaskStuService.selectSysTaskStuList(sysTaskStu);
        ExcelUtil<SysTaskStu> util = new ExcelUtil<SysTaskStu>(SysTaskStu.class);
        return util.exportExcel(list, "teachecktask");
    }

    /**
     * 新增审核课题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审核课题
     */
    @Log(title = "审核课题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.insertSysTaskStu(sysTaskStu));
    }

    /**
     * 修改审核课题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id);
        mmap.put("sysTaskStu", sysTaskStu);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核课题
     */
    @Log(title = "审核课题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTaskStu sysTaskStu)
    {
        return toAjax(sysTaskStuService.updateSysTaskStu(sysTaskStu));
    }

    /**
     * 删除审核课题
     */
    @Log(title = "审核课题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTaskStuService.deleteSysTaskStuByIds(ids));
    }

    /**
     * 修改课题状态为通过
     */
    @PostMapping("/editid1")
    @ResponseBody
    public AjaxResult editid1(int id)
    {
        Long id1 = (long)id;
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id1);
        System.out.println(id);
        sysTaskStu.setStatus(1);
        sysTaskStuService.updateSysTaskStu(sysTaskStu);
        return toAjax(true);
    }

    /**
     * 修改课题状态为修改
     */
    @PostMapping("/editid2")
    @ResponseBody
    public AjaxResult editid2(int id)
    {
        Long id1 = (long)id;
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id1);
        sysTaskStu.setStatus(2);
        sysTaskStuService.updateSysTaskStu(sysTaskStu);
        return toAjax(true);
    }

    /**
     * 修改课题状态为驳回
     */
    @PostMapping("/editid3")
    @ResponseBody
    public AjaxResult editid3(int id)
    {
        Long id1 = (long)id;
        SysTaskStu sysTaskStu = sysTaskStuService.selectSysTaskStuById(id1);
        sysTaskStu.setStatus(3);
        sysTaskStuService.updateSysTaskStu(sysTaskStu);
        return toAjax(true);
    }
}
